package com.lhn.bbs.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lhn.bbs.dto.ActionResult;
import com.lhn.bbs.entity.Theme;
import com.lhn.bbs.mapper.ThemeDao;
import com.lhn.bbs.mapper.UserDao;
import com.lhn.bbs.service.ThemeService;
import com.lhn.bbs.utils.JiebaUtils;
import com.lhn.bbs.utils.SensitiveFilter;
import com.lhn.bbs.vo.ReplyVo;
import com.lhn.bbs.vo.ThemeAddVo;
import com.lhn.bbs.vo.ThemeOneVo;
import com.lhn.bbs.vo.ThemeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ Author :lihainan
 * @ ClassNmae :ThemeServiceImpl
 * @ Description :TODO
 * @ Date :2021/4/1
 * @ Version 1.0
 */
@Service
@Slf4j
public class ThemeServiceImpl implements ThemeService {

    @Autowired(required = false)
    private ThemeDao  themeDao ;

    @Autowired
    private SensitiveFilter sensitiveFilter;

    @Autowired
    private JiebaUtils jiebaUtils ;

    @Autowired(required = false)
    private UserDao userDao;

    /**
     * 查询最新主题帖
     * @param page  页数
     * @param limit   分页条数
     * @return
     */
    @Override
    public ActionResult findNewThemeAll(Integer page, Integer limit) {

        PageHelper.startPage(page,limit);
        List<ThemeVo> themeVos =  themeDao.selectNewThemeAll();
        for(ThemeVo themeVo:themeVos){
            int thememun = themeDao.findByIdcount(themeVo.getThemeid());
            themeVo.setThemenum(thememun);
        }

//        log.info("========= " + themeVos);

        PageInfo<ThemeVo> pageInfo=new PageInfo(themeVos);
        long total = pageInfo.getTotal();
        if(themeVos != null ){
//            log.info("=========="+pageInfo);
            return new ActionResult(200 ,"查询成功",pageInfo ,total);
        }
        return new ActionResult(201,"查询失败",null,null);
    }



    /**
     * 查询帖子详情
     * @param themeid
     * @return
     */
    @Override
    public ActionResult findNewThemeOne(Integer themeid,Integer page, Integer limit) {

        try{
            //查询帖子详情
            ThemeOneVo themeOneVo =  themeDao.selectThemeOne(themeid);
//            log.info("=========="+themeOneVo);
            //回帖数量查询
            int count = themeDao.selectcount(themeid);

            PageHelper.startPage(page,limit);
            //查询帖子相关的所有回复内容
            List<ReplyVo> replyVo = themeDao.selectReply(themeid);

            // 获取所有的回复评论

            List<ReplyVo> replys = themeDao.selectReply(themeid);
            StringBuffer stringBuffer = new StringBuffer();
            for(ReplyVo reply : replys){
                stringBuffer = stringBuffer.append(reply.getReplycontent());
                log.info(reply.toString());
            }

            // 提取帖子相关的回复内容的高频词
            String str = stringBuffer.toString();
            String higeword = jiebaUtils.higeword(str);
            log.info("帖子内容====="+str);
            if(higeword == null){
                higeword = "评论中没有提取到高频词,快了评论吧!";
            }

            log.info("高频词:"+higeword);
            PageInfo<ThemeVo> pageInfo=new PageInfo(replyVo);

            long total = pageInfo.getTotal();

            Map<String, Object> map = new HashMap<>();
            map.put("theme",themeOneVo);
            map.put("count",count);
            map.put("replyvo",pageInfo);
            map.put("higeword",higeword);

            return  new ActionResult(200,"查询成功",map,total);
        }catch (Exception e){
            log.info("========="+"出错了");
            return  new ActionResult(202,"查询失败",null,null);

        }
    }

    /**
     * 根据板块id查询主题
     * @param page
     * @param limit
     * @param plateid
     * @return
     */
    @Override
    public ActionResult findpThemeAll(Integer page, Integer limit, Integer plateid) {
        PageHelper.startPage(page,limit);
        List<ThemeVo> themeVos =  themeDao.selectpThemeAll(plateid);
        for(ThemeVo themeVo:themeVos){
            int thememun = themeDao.findByIdcount(themeVo.getThemeid());
            themeVo.setThemenum(thememun);
        }

        PageInfo<ThemeVo> pageInfo=new PageInfo(themeVos);
        long total = pageInfo.getTotal();
        if(themeVos != null ){
//            log.info("=========="+pageInfo);
            return new ActionResult(200 ,"查询成功",pageInfo ,total);
        }
        return new ActionResult(201,"查询失败",null,null);
    }

    /**
     * 主题帖的发布
     * @param themeAddVo
     * @return
     */
    @Override
    public ActionResult themeAdd(ThemeAddVo themeAddVo) {
        try{
            // 转义HTML标记
            themeAddVo.setThemetitle(HtmlUtils.htmlEscape(themeAddVo.getThemetitle()));
            themeAddVo.setThemecontent(HtmlUtils.htmlEscape(themeAddVo.getThemecontent()));
            // 过滤敏感词
            themeAddVo.setThemetitle(sensitiveFilter.filter(themeAddVo.getThemetitle()));
            themeAddVo.setThemecontent(sensitiveFilter.filter(themeAddVo.getThemecontent()));

            long  l = themeDao.saveTheme(themeAddVo);
            log.info("==========="+l);

            return new ActionResult(200,"添加成功",null,null);
        }catch (Exception e){
            log.info("========="+e.getMessage());
            return new ActionResult(201,"添加失败",null,null);
        }
    }

    /**
     * 根据标题模糊查询
     * @param likename
     * @return
     */
    @Override
    public ActionResult likefindByname(String likename,Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        List<ThemeVo>  list = themeDao.selectlike(likename);
        for(ThemeVo themeVo:list){
            int thememun = themeDao.findByIdcount(themeVo.getThemeid());
            themeVo.setThemenum(thememun);
        }

        PageInfo<ThemeVo> pageInfo=new PageInfo(list);
        long total = pageInfo.getTotal();
        if(list != null){
//            log.info("++++++"+list);
            return new ActionResult(200,"success",pageInfo,total);
        }

        return new ActionResult(201,"查询失败",null,null);
    }

    /**
     * 根据主题创建人查询
     * @param page
     * @param limit
     * @param userid
     * @return
     */
    @Override
    public ActionResult findOneThemeAll(Integer page, Integer limit, Integer userid) {
        PageHelper.startPage(page,limit);
        List<ThemeVo> themeVoList = themeDao.findByuserid(userid);

        for(ThemeVo themeVo:themeVoList){
            int thememun = themeDao.findByIdcount(themeVo.getThemeid());
            themeVo.setThemenum(thememun);
        }
        PageInfo<ThemeVo> pageInfo=new PageInfo(themeVoList);
        long total = pageInfo.getTotal();
        // 查询用户发帖的个数
        int number = themeDao.findThemenumberByUserid(userid);

        userDao.editthemenum(number,userid);

        Map<String,Object> map = new HashMap<String,Object>();
        map.put("number",number);
        map.put("list",pageInfo);

        if(themeVoList != null){
//            log.info("++++++"+themeVoList);
            return new ActionResult(200,"success",map,total);
        }

        return new ActionResult(201,"查询失败",null,null);
    }

    /**
     * 根据themeid删除帖子
     * @param themeid
     * @return
     */
    @Override
    public ActionResult deleteTheme(Integer themeid) {
        try{
            Boolean b = themeDao.deleteThemeById(themeid);
            Boolean b2 = themeDao.deleteReplyByThemeId(themeid);
            return new ActionResult(200,"删除成功",null,null);
        }catch (Exception e){
            log.info("===========删除帖子出错了"+e.getMessage());
            return new ActionResult(201,"删除失败",null,null);
        }
    }

    /**
     * 班级主题的发布
     * @param themeAddVo
     * @return
     */
    @Override
    public ActionResult themeClassAdd(ThemeAddVo themeAddVo) {
        try{
            // 转义HTML标记
            themeAddVo.setThemetitle(HtmlUtils.htmlEscape(themeAddVo.getThemetitle()));
            themeAddVo.setThemecontent(HtmlUtils.htmlEscape(themeAddVo.getThemecontent()));
            // 过滤敏感词
            themeAddVo.setThemetitle(sensitiveFilter.filter(themeAddVo.getThemetitle()));
            themeAddVo.setThemecontent(sensitiveFilter.filter(themeAddVo.getThemecontent()));

            long  l = themeDao.saveClassTheme(themeAddVo);
            log.info("==========="+l);

            return new ActionResult(200,"添加成功",null,null);
        }catch (Exception e){
            log.info("========="+e.getMessage());
            return new ActionResult(201,"添加失败",null,null);
        }
    }
}
